name: CI Build

env:
  GITHUB_REF: "${{ github.ref }}"
  APR_HOME16: ${{ github.workspace }}\APR\
  RELEASE_DIR: artifacts
  RELEASE_BIN: grok
  WINDOWS_TARGET: x86_64-pc-windows-msvc
  MACOS_TARGET: x86_64-apple-darwin
  LINUX_TARGET: x86_64-unknown-linux-gnu
  RELEASER_VER: 0.10.6

on:
  push:
    branches:
      - master
      - develop
    tags:
      - "v*" # Run when tag matches v*, i.e. v1.0, v20.15.10
  pull_request:
    branches: [ master ]

jobs:
  build_windows:

    runs-on: windows-latest

    steps:
      - uses: actions/checkout@v3
      - name: Query version number
        id: get_version
        shell: bash
        run: |
          echo "using version tag ${GITHUB_REF:11}"
          echo ::set-output name=version::"${GITHUB_REF:11}"
      - name: Install Winflexbison
        run: |
          choco install winflexbison
      - name: Create generated dir
        run: |
          mkdir ${{ github.workspace }}\src\grok\generated
      - name: Download APR
        run: |
          curl -L  -o apr-vs2022.zip "https://drive.google.com/uc?export=download&id=1S0K5zTTn-Y_X2Di1racXPy4yLQ0kwHT4"
          7z x -o${{ env.APR_HOME16 }} apr-vs2022.zip
      - name: flex and bison
        run: |
          win_flex.exe --fast --wincompat --outfile="src/grok/generated/grok.flex.c" src/grok/grok.lex
          win_bison.exe --output="src/grok/generated/grok.tab.c" -dy src/grok/grok.y
      - name: Add msbuild to PATH
        uses: microsoft/setup-msbuild@v1.1
        with:
          msbuild-architecture: x64
      - name: Build
        uses: lukka/run-cmake@v3
        with:
          cmakeListsOrSettingsJson: CMakeListsTxtAdvanced
          cmakeListsTxtPath: ${{ github.workspace }}\CMakeLists.txt
          cmakeBuildType: Release
          buildWithCMakeArgs: "-- -v:n -p:Configuration=Release"
          buildDirectory: ${{ runner.temp }}\build
          buildWithCMake: true
          cmakeAppendedArgs: '-DCMAKE_BUILD_TYPE=Release -Ax64'
        env:
          GROK_VERSION: ${{ steps.get_version.outputs.VERSION }}
      - name: Unit Tests
        shell: cmd
        run: ${{ runner.temp }}\build\Release\_tst.exe
      - name: Create artifact directory
        if: startsWith(github.ref, 'refs/tags/v')
        run: |
          mkdir ${{ env.RELEASE_DIR }}
      - name: Install
        if: startsWith(github.ref, 'refs/tags/v')
        run: |
          cmake --install ${{ runner.temp }}\build
      - name: Packaging
        if: startsWith(github.ref, 'refs/tags/v')
        run: |
          tar -cvzf .\${{ env.RELEASE_DIR }}\${{ env.RELEASE_BIN }}-${{ steps.get_version.outputs.VERSION }}-${{ env.WINDOWS_TARGET }}.tar.gz -C .\bin *
      - uses: actions/upload-artifact@v2
        if: startsWith(github.ref, 'refs/tags/v')
        with:
          name: windows
          path: .\${{ env.RELEASE_DIR }}

  build_linux:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
      - name: Query version number
        id: get_version
        shell: bash
        run: |
          echo "using version tag ${GITHUB_REF:11}"
          echo ::set-output name=version::"${GITHUB_REF:11}"
      - name: Create generated dir
        run: |
          mkdir ${{ github.workspace }}/src/grok/generated
      - name: Install Linux packages
        run: sudo apt update && sudo apt install -y --no-install-recommends apache2-dev libapr1-dev libaprutil1-dev
      - name: flex and bison
        run: |
          flex --fast --outfile="src/grok/generated/grok.flex.c" src/grok/grok.lex
          bison --output="src/grok/generated/grok.tab.c" -dy src/grok/grok.y
      - name: Build
        uses: lukka/run-cmake@v3
        with:
          cmakeListsOrSettingsJson: CMakeListsTxtBasic
          cmakeListsTxtPath: ${{ github.workspace }}/CMakeLists.txt
          cmakeBuildType: Release
          buildWithCMakeArgs: "-- -v"
          buildDirectory: ${{ runner.temp }}/build
          buildWithCMake: true
        env:
          APR_HOME: /usr/
          GROK_VERSION: ${{ steps.get_version.outputs.VERSION }}
      - name: Unit Tests
        run: ${{ runner.temp }}/build/_tst
      - name: Create artifact directory
        if: startsWith(github.ref, 'refs/tags/v')
        run: |
          mkdir ${{ env.RELEASE_DIR }}
      - name: Install
        if: startsWith(github.ref, 'refs/tags/v')
        run: |
          cmake --install ${{ runner.temp }}/build
      - name: Packaging
        if: startsWith(github.ref, 'refs/tags/v')
        run: |
          7z a -ttar -so -an ${{ github.workspace }}/bin/* | 7z a -si ./${{ env.RELEASE_DIR }}/${{ env.RELEASE_BIN }}-${{ steps.get_version.outputs.VERSION }}-${{ env.LINUX_TARGET }}.tar.gz
      - uses: actions/upload-artifact@v2
        if: startsWith(github.ref, 'refs/tags/v')
        with:
          name: linux
          path: ./${{ env.RELEASE_DIR }}

  build_macos:

    runs-on: macos-11

    steps:
      - uses: actions/checkout@v3
      - name: Query version number
        id: get_version
        shell: bash
        run: |
          echo "using version tag ${GITHUB_REF:11}"
          echo ::set-output name=version::"${GITHUB_REF:11}"
      - name: Create generated dir
        run: |
          mkdir ${{ github.workspace }}/src/grok/generated
      - name: install tools and libs
        run: |
          brew install apr
          brew install apr-util
          brew install flex
          brew install bison
      - name: flex and bison
        run: |
          export PATH="/usr/local/opt/flex/bin:/usr/local/opt/bison/bin:$PATH"
          flex --fast --outfile="src/grok/generated/grok.flex.c" src/grok/grok.lex
          bison --output="src/grok/generated/grok.tab.c" -dy src/grok/grok.y
      - name: Build
        uses: lukka/run-cmake@v3
        with:
          cmakeListsOrSettingsJson: CMakeListsTxtBasic
          cmakeListsTxtPath: ${{ github.workspace }}/CMakeLists.txt
          cmakeBuildType: Release
          buildWithCMakeArgs: "-- -v"
          buildDirectory: ${{ runner.temp }}/build
          buildWithCMake: true
        env:
          APR_HOME: /usr/local/opt/
          CMAKE_INCLUDE_PATH: /usr/local/opt/flex/include
          CMAKE_LIBRARY_PATH: /usr/local/opt/flex/lib;/usr/local/opt/bison/lib
          GROK_VERSION: ${{ steps.get_version.outputs.VERSION }}
      - name: Unit Tests
        run: ${{ runner.temp }}/build/_tst
      - name: Install p7zip (MacOS)
        if: startsWith(github.ref, 'refs/tags/v')
        run: brew install p7zip
      - name: Create artifact directory
        if: startsWith(github.ref, 'refs/tags/v')
        run: |
          mkdir ${{ env.RELEASE_DIR }}
      - name: Install
        if: startsWith(github.ref, 'refs/tags/v')
        run: |
          cmake --install ${{ runner.temp }}/build
      - name: Packaging
        if: startsWith(github.ref, 'refs/tags/v')
        run: |
          7z a -ttar -so -an ${{ github.workspace }}/bin/* | 7z a -si ./${{ env.RELEASE_DIR }}/${{ env.RELEASE_BIN }}-${{ steps.get_version.outputs.VERSION }}-${{ env.MACOS_TARGET }}.tar.gz
      - uses: actions/upload-artifact@v2
        if: startsWith(github.ref, 'refs/tags/v')
        with:
          name: macos
          path: ./${{ env.RELEASE_DIR }}

  release:
    name: GitHub Release
    needs: [ build_windows, build_macos, build_linux ]
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-latest
    steps:
      - name: Query version number
        id: get_version
        shell: bash
        run: |
          echo "using version tag ${GITHUB_REF:11}"
          echo ::set-output name=version::"${GITHUB_REF:11}"

      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ steps.get_version.outputs.VERSION }}
          release_name: ${{ steps.get_version.outputs.VERSION }}

      - name: Download Linux tarball
        uses: actions/download-artifact@v1
        with:
          name: linux

      - name: Download Windows zip
        uses: actions/download-artifact@v1
        with:
          name: windows

      - name: Download MacOS zip
        uses: actions/download-artifact@v1
        with:
          name: macos

      - name: Create fake Cargo.toml
        run: |
          cat >Cargo.toml <<EOL
          [package]
          name = "${{ env.RELEASE_BIN }}"
          version = "${{ steps.get_version.outputs.VERSION }}"
          description = "Regular expressions macro engine. GROK is a tool like UNIX grep on steroids"
          authors = ["egoroff <egoroff@gmail.com>"]
          homepage = "https://github.com/aegoroff/grok"
          license = "MIT"
          EOL

      - name: Download Releaser
        run: |
          curl -L  -o releaser.tar.gz https://github.com/aegoroff/releaser/releases/download/${{ env.RELEASER_VER }}/releaser-${{ env.RELEASER_VER }}-x86_64-unknown-linux-musl.tar.gz
          tar -xf releaser.tar.gz
      - name: Create brew formula
        run: ${{ github.workspace }}/releaser b -c . -b https://github.com/${{ github.repository }}/releases/download/${{ steps.get_version.outputs.VERSION }}/ -l './linux/' -m './macos/' -u ${{ env.RELEASE_BIN }}.rb
      - name: Publish brew formula
        uses: dmnemec/copy_file_to_another_repo_action@main
        env:
          API_TOKEN_GITHUB: ${{ secrets.GH_TOKEN }}
        with:
          github_server: 'github.com'
          source_file: ${{ env.RELEASE_BIN }}.rb
          destination_repo: 'aegoroff/homebrew-tap'
          user_email: 'egoroff@email.com'
          user_name: 'aegoroff'
          commit_message: "release: ${{ steps.get_version.outputs.VERSION }}"

      - name: Create scoop json
        run: ${{ github.workspace }}/releaser s -c . -b https://github.com/${{ github.repository }}/releases/download/${{ steps.get_version.outputs.VERSION }}/ -i './windows/' -e ${{ env.RELEASE_BIN }}.exe -u ${{ env.RELEASE_BIN }}.json
      - name: Publish scoop json
        uses: dmnemec/copy_file_to_another_repo_action@main
        env:
          API_TOKEN_GITHUB: ${{ secrets.GH_TOKEN }}
        with:
          github_server: 'github.com'
          source_file: ${{ env.RELEASE_BIN }}.json
          destination_repo: 'aegoroff/scoop-bucket'
          user_email: 'egoroff@email.com'
          user_name: 'aegoroff'
          commit_message: "release: ${{ steps.get_version.outputs.VERSION }}"

      - name: Release Linux tarball
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./linux/${{ env.RELEASE_BIN }}-${{ steps.get_version.outputs.VERSION }}-${{ env.LINUX_TARGET }}.tar.gz
          asset_content_type: application/gzip
          asset_name: ${{ env.RELEASE_BIN }}-${{ steps.get_version.outputs.VERSION }}-${{ env.LINUX_TARGET }}.tar.gz

      - name: Release Windows zip
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./windows/${{ env.RELEASE_BIN }}-${{ steps.get_version.outputs.VERSION }}-${{ env.WINDOWS_TARGET }}.tar.gz
          asset_content_type: application/gzip
          asset_name: ${{ env.RELEASE_BIN }}-${{ steps.get_version.outputs.VERSION }}-${{ env.WINDOWS_TARGET }}.tar.gz

      - name: Release MacOS tarball
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./macos/${{ env.RELEASE_BIN }}-${{ steps.get_version.outputs.VERSION }}-${{ env.MACOS_TARGET }}.tar.gz
          asset_content_type: application/gzip
          asset_name: ${{ env.RELEASE_BIN }}-${{ steps.get_version.outputs.VERSION }}-${{ env.MACOS_TARGET }}.tar.gz
